/**
 * jims
 */
package com.jims.master.modules.doctor.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.doctor.entity.HisDataIcd10;
import com.jims.master.modules.doctor.service.HisDataIcd10Service;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * Controller
 * @author chentao
 * @version 2017-03-21
 */
@Controller
@RequestMapping(value = "${adminPath}/doctor/hisDataIcd10")
public class HisDataIcd10Controller extends BaseController {

	@Autowired
	private HisDataIcd10Service hisDataIcd10Service;
	
	@ModelAttribute
	public HisDataIcd10 get(@RequestParam(required=false) String id) {
		HisDataIcd10 entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hisDataIcd10Service.get(id);
		}
		if (entity == null){
			entity = new HisDataIcd10();
		}
		return entity;
	}

	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(HisDataIcd10 hisDataIcd10, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HisDataIcd10> page = hisDataIcd10Service.findPage(new Page<HisDataIcd10>(request, response), hisDataIcd10); 
		model.addAttribute("page", page);
		return "modules/doctor/hisDataIcd10Index";
	}

	/**
	 *his_data_icd10自动补全
	 * @param q
	 * @author chentao
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "automaticIcd")
	@ResponseBody
	public List<HisDataIcd10> list(String q,String limit){
		if(q!=null&&!q.equals("")){
			try {
				q = URLDecoder.decode(q,"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		List automaticIcdList = hisDataIcd10Service.hisDataIcdName(q,limit);
		return automaticIcdList;
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(HisDataIcd10 hisDataIcd10, Model model) {
		model.addAttribute("hisDataIcd10", hisDataIcd10);
		return "modules/doctor/hisDataIcd10Form";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(HisDataIcd10 hisDataIcd10, Model model) {
	    StringData data=new StringData();
		int i=hisDataIcd10Service.save(hisDataIcd10);
        if(i>0){
            data.setCode("success");
            data.setData("保存icd-10编码成功");
        }else{
            data.setCode("error");
            data.setData("保存icd-10编码失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(HisDataIcd10 hisDataIcd10) {
	    StringData data=new StringData();
		int i=hisDataIcd10Service.delete(hisDataIcd10);
        if(i>0){
            data.setCode("success");
            data.setData("删除icd-10编码成功");
        }else{
            data.setCode("error");
            data.setData("删除icd-10编码失败");
        }
		return data;
	}

}